package com.mofofa.maozy.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseDialogListAction
import com.mofofa.maozy.data.DataCustomizeContact
import com.mofofa.maozy.databinding.DialogMaozyPopListBinding
import com.mofofa.maozy.utils.AppConfigUtil

class PopListDialog : DialogFragment(), BaseDialogListAction {

    companion object {
        fun showDialog(
            fragmentManager: FragmentManager
        ) {
            val dialog = PopListDialog()
            dialog.showNow(fragmentManager, PopListDialog::class.java.simpleName)
        }
    }

    lateinit var mMaozyViewMaozyBinding: DialogMaozyPopListBinding
    lateinit var mMaozyPopMaozyListMaozyAdapter: PopListAdapter

    private val mMaozyDataMaozyList = mutableListOf<PopListItem>()
    private var mMaozyUIMaozyTag: String = ""
    private var mMaozyOnMaozyItemMaozyClickMaozyListener: (String, PopListItem) -> Unit =
        { tag, item -> }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mMaozyViewMaozyBinding = DialogMaozyPopListBinding.inflate(layoutInflater)

        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mMaozyViewMaozyBinding.textDialogPopListTitle.text = mMaozyUIMaozyTag
        mMaozyPopMaozyListMaozyAdapter.notifyDataSetChanged()
    }

    override fun initMaozySubviews() {
        try {
            val list = mutableListOf<DataCustomizeContact>()
            val contacts = AppConfigUtil.getCashArrwoCustomerMaozyServiceMaozyCnotacts()
            list.addAll(contacts)

            mMaozyPopMaozyListMaozyAdapter =
                PopListAdapter(mMaozyDataMaozyList)
            mMaozyPopMaozyListMaozyAdapter.setMaozyOnMaozyItemMaozyClickMaozyListener { i, cashVistaPopListItem ->
                mMaozyOnMaozyItemMaozyClickMaozyListener.invoke(
                    mMaozyUIMaozyTag,
                    cashVistaPopListItem
                )
                dismiss()
            }

            mMaozyViewMaozyBinding.rcDialogPopListList.layoutManager =
                LinearLayoutManager(requireContext())
            mMaozyViewMaozyBinding.rcDialogPopListList.adapter =
                mMaozyPopMaozyListMaozyAdapter

            mMaozyViewMaozyBinding.imageDialogPopListClose.setOnClickListener { dismiss() }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyDataMaozyList(dataList: List<PopListItem>, gTag: String) {
        mMaozyUIMaozyTag = gTag
        mMaozyDataMaozyList.clear()
        mMaozyDataMaozyList.addAll(dataList)
    }

    override fun showMaozyWithMaozyDataMaozyList(
        fragmentManager: FragmentManager,
        dataList: List<PopListItem>,
        gTag: String
    ) {
        mMaozyUIMaozyTag = gTag
        mMaozyDataMaozyList.clear()
        mMaozyDataMaozyList.addAll(dataList)
        showNow(fragmentManager, PopListDialog::class.java.simpleName)
    }

    override fun setMaozyOnMaozyItemMaozyClickMaozyListener(listener: (String, PopListItem) -> Unit) {
        mMaozyOnMaozyItemMaozyClickMaozyListener = listener
    }

}